/**
 * 消息窗口
 */
var NoticeWindow = Window.extend({
    _ccsFileName :res.NoticeWindow_json,
    _FontSize:26,
    _FontColor:cc.color("#7A5535"),
    /**
     * 初始化数据
     * @param {Array} data 数据数组
     * @private
     */
    initWindow: function (data) {
        var self = this;
        self._data = data;
        self._listBtnScroll = self._nodeList['root.listBtnScroll'];
        self._listBtnNode = self._listBtnScroll['listBtnNode'];
        self._touchNode = self._listBtnNode["touchNode"];
        self._touchNode.setVisible(false);
        self._dataScroll = self._nodeList["root.dataScroll"];
        self.btn_Hight = self._touchNode.getContentSize().height;
        self.offset_H = 5;
        self._initBtn();
    },
    /**
     * 初始化按钮
     */
    _initBtn: function () {
        var self = this;
        //数据导致异常
        if (self._data == null || self._data == undefined) return;
        //实际scroll高度
        var btnScrollH = (self._data.length) * (self.btn_Hight + self.offset_H);
        var interval = (self.btn_Hight + self.offset_H);
        for (var i = 0; i < self._data.length; i++) {
            (function (i) {
                var button = self._listBtnNode["touch" + i];
                self._data[i].ID = self._data[i]['_id'];
                self._data[i].Name = self._data[i].Name.replace(/\s+/g, "");
                if (!button) {
                    button = duplicateNode(self._touchNode)
                    button.setName("touch" + i);
                    button.setPositionY(self._touchNode.getPositionY() - interval * i);
                    button.getChildByName('unselect').getChildByName('word').setString(self._data[i].Name);
                    button.getChildByName('select').getChildByName('word').setString(self._data[i].Name);
                    button.setVisible(true);
                    var hasread = cc.sys.localStorage.getItem("notice" + self._data[i]["TimeInMillionSecond"] + gameData.uid);
                    button.getChildByName('weidu').setVisible(hasread != 1);
                    var data = {};
                    data.btnId = self._data[i].ID;
                    button.setUserData(data);
                    self._listBtnNode.addChild(button);
                    self._listBtnNode["touch" + i] = button;
                }

                TouchUtil.setOnclickListener(button, function () {
                    self.changeNotice(i);
                }, {"swallowTouches": false});
            })(i);
        }


        var width = self._listBtnScroll.getContentSize().width;
        var height = btnScrollH > self._listBtnScroll.getContentSize().height ? btnScrollH : self._listBtnScroll.getContentSize().height
        self._listBtnScroll.setInnerContainerSize(cc.size(width, height));
        self._listBtnNode.setPositionY(height);
        if (self._data.length > 0) {
            self.changeNotice(0);
        }
    },
    /**
     * 切换信息
     * @param tab
     */
    changeNotice: function (tab) {
        var self = this;
        var id = self._data[tab].ID;
        cc.sys.localStorage.setItem("notice" + self._data[tab]["TimeInMillionSecond"] + gameData.uid, "1");
        self.setContent(self._data[tab]["Details"]);
        self._listBtnNode["touch" + tab]["weidu"].setVisible(false);
        for (var i = 0; i < self._data.length; i++) {
            (function (i) {
                var button = self._listBtnNode["touch" + i];
                var data = button.getUserData();
                button.getChildByName('unselect').setVisible(data.btnId !== id);
                button.getChildByName('select').setVisible(data.btnId === id);
            })(i);
        }
        ;
        self._upDateMessageNum();
    },
    /**
     * 设置信息文本
     * @param text
     */
    setContent: function (text) {
        var self = this;
        self._dataScroll.removeAllChildren();
        var newtext = text.replace(/\\n/g, "\n");
        var content = new cc.LabelTTF();
        content.setFontSize(self._FontSize);
        content.boundingWidth = self._dataScroll.getContentSize().width - 20;
        content.setColor(self._FontColor);
        content.setString(newtext);
        var posy = content.getContentSize().height / 2;
        if (content.getContentSize().height < self._dataScroll.getContentSize().height) {
            posy = self._dataScroll.getContentSize().height - posy;
        }
        content.setPosition(cc.p(content.boundingWidth / 2 + 10, posy-5));
        self._dataScroll.addChild(content);
        self._dataScroll.setInnerContainerSize(cc.size(self._dataScroll.getContentSize().width, content.getContentSize().height));
    },
    /**
     * 更新信息数量
     * @protected
     */
    _upDateMessageNum: function () {
        var notReadNum = 0;
        for (var i = 0; i < this._data.length; i++) {
            var hasread = cc.sys.localStorage.getItem('notice' + this._data[i]['TimeInMillionSecond'] + gameData.uid);
            if (hasread == undefined || hasread == null || hasread == '') {
                notReadNum++;
            }
        }
        eventManager.dispatchEvent(EventKey.UpdateMessageNum, notReadNum);
    },

    /**
     * 读消息
     * */
    _readNotice: function (ts) {
        cc.sys.localStorage.setItem('notice' + ts + gameData.uid, true);
    },

    /**
     * 消息是否已读
     * @protected
     * */
    _isRead: function (ts) {
        var hasread = cc.sys.localStorage.getItem('notice' + ts + gameData.uid);
        if (hasread == undefined || hasread == null || hasread == '') {
            return false;
        }
        return true;
    }
});